<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/22
 * Time: 18:49
 */

namespace Admin\Controller;

use Think\Page;
class ExamController extends AdminController
{

    public function index()
    {
        $nickname = trim(I('get.nickname',''));
        if ($nickname){
            $map['nickname'] = array('like', "%{$nickname}%");
        }
        $p = I('p',0);
        $type = I('t',1); // 0-充值, 1-提现
        $status = I('s',1); //0-失败, 1-等待, 2-成功
        if ($status!=0){
            $map['type'] = $type;
        }
        $map['status'] = $status;
        $page_size = 15;
        $Exam = M('Exam');
        $count = $Exam->where($map)->count();
        $list = $Exam->where($map)->page($p . ',' . $page_size)->order('create_time desc')->select();
        $page = new Page($count, $page_size);
        $this->assign('_page', $page->show());
        $this->assign('list', $list);
        if ($type==1&&$status==1){
            $meta_title = '提现审核列表';
        }else if ($status==0){
            $meta_title = '失败记录';
        }else if ($type==1&&$status==2){
            $meta_title = '提现记录';
        }else if ($type==0){
            $meta_title = '充值记录';
        }
        $this->assign('meta_title', $meta_title);
        $this->display();
    }

    //详细信息
    public function detail(){
        $id = I('id','');
        $map['id'] = $id;
        $info = M('Exam')->where($map)->find();
        $this->assign('info',$info);
        $this->display();
    }

    //审核提交
    public function deal(){
        $id = I('id','');
        $deal = I('deal',-1);
        $exam = M('Exam')->where('id='.$id)->find();
        if ($exam){
            $map['uid'] = $exam['uid'];
            if ($deal==1){
                $data1['status'] = 2;
                $data1['mid'] = UID;
                $data1['update_time'] = time();

                $man = M('Member')->where('uid='.UID)->find();
                if (!$man){
                    $this->error('管理员信息错误!',U('index?t=1&s=1'));
                    return;
                }
                $data1['mname']=$man['nickname'];

                $tmp = M('Member')->where("uid=".$exam['uid'])->field('withdraw')->find();
                $data2['withdraw'] = floatval($tmp['withdraw'])-floatval($exam['count']);
                if ($data2['withdraw']<0){
                    $this->error('操作失败!',U('index?t=1&s=1'));
                    return;
                }
                $m1 = M('Exam');
                $m2 = M('Member');
                $m1->startTrans();
                $resE = $m1->where("id='$id'")->data($data1)->save();
                $resM = $m2->where("uid=".$exam['uid'])->data($data2)->save();

                if ($resE&&$resM){
                    $m1->commit();
                    $this->success('审核提交成功! 允许提现!',U('index?t=1&s=1'));
                }else{
                    $m1->rollback();
                    $this->error('审核提交失败! -1',U('index?t=1&s=1'));
                }
            }else if($deal==0){
                $member = M('Member')->where('uid='.$exam['uid'])->field('withdraw,account')->find();
                if ($member){
                    $data1['status'] = 0;
                    $data1['mid'] = UID;
                    $data1['update_time'] = time();
                    $man = M('Member')->where('uid='.UID)->find();
                    if (!$man){
                        $this->error('管理员信息错误!',U('index?t=1&s=1'));
                        return;
                    }
                    $data1['mname']=$man['nickname'];

                    $data2['account'] = floatval($member['account']) + $exam['count'];
                    $data2['withdraw'] = floatval($member['withdraw']) - $exam['count'];

                    $m1 = M('Exam');
                    $m2 = M('Member');
                    $m1->startTrans();
                    $resE = $m1->where("id='$id'")->data($data1)->save();
                    $resM = $m2->where('uid='.$exam['uid'])->data($data2)->save();
                    if ($resE&&$resM){
                        $m1->commit();
                        $this->success('审核提交成功! 拒绝提现!',U('index?t=1&s=1'));
                    }else{
                        $m1->rollback();
                        $this->error('审核提交失败! -2',U('index?t=1&s=1'));
                    }
                }else{
                    $this->error('审核提交失败! 会员信息错误!',U('index?t=1&s=1'));
                }

            }else{
                $this->error('审核提交失败! 非法状态!',U('index?t=1&s=1'));
            }
        }else{
            $this->error('操作失败!',U('index?t=1&s=1'));
        }
    }
}